<?php
/*
 AJAX.PHP by martin $ 2009/04/22 01:20:33
*/
define('DIR', dirname(__FILE__).'/');

include_once (DIR.'owner/ini.inc.php');
include_once (DIR.'utils.php');
include_once (DIR.'session.class.php');
include_once (DIR.'owner/usrlist.ini.php');
$hasShareUsr = !empty($USR_LIST) ? 1 : 0;
if(QUERY == '') return;

if(!defined('ADMIN')) define('ADMIN', Auth() ? TRUE : FALSE);

if($mb==TRUE){
 @mb_language("Japanese");
 @mb_internal_encoding(ENCODE);
} 

header ("Content-Type: text/html; charset=".ENCODE);

$q = explode('&amp;', QUERY);
if(strstr($q[0], '=')) list(, $type) = explode('=', $q[0]);
if(isset($q[1]) && strstr($q[1], '=')) list(, $aim) = explode('=', $q[1]);

$smiley = DIR.'owner/smileyJSON.js';

if(empty($theme) || !is_file(PATH.$theme) || USE_THEME_CHANGER==FALSE){
 include_once(PATH.OD.'theme.ini.php');
 $theme = array_values($THEME_LIST); $theme = $theme[0];
}

switch ($type){
 case 'text' : case 'clone' :
  if(!preg_match('{^(js|comments)}', $aim)) exit;
  exit(get_file_content($aim)); break;
 case 'info' :
  header ("Content-Type: text/javascript; charset=".ENCODE);
  $ref = getenv('HTTP_REFERER');
  if(empty($ref)) exit;
  if(strpos($ref, ROOT_PATH) != 0) exit;
  if(!empty($_COOKIE['PPBLOG_AUTO_LOGIN'])){
   $salt = substr($_COOKIE['PPBLOG_AUTO_LOGIN'], -10);
   $hash = PHP_VERSION >= '4.3.0' ? sha1($salt.OPASS.$salt).$salt : md5($salt.OPASS.$salt).$salt;
   $auto = ($hash===$_COOKIE['PPBLOG_AUTO_LOGIN']) ? 1 : 0;
  } else $auto = 0;
  $auto = ($hasShareUsr) ? 0 : $auto; // always
  $s = 'ONAME = "'.ONAME.'";ADMIN='.(ADMIN?1:0).';AUTO_LOGIN = '.$auto.';SOLO='.($hasShareUsr ? 0 : 1).';OADMIN='.(isOwnerLoggedIn()?1:0).';UNAME="'.(isset($SESSION) && ADMIN ?$SESSION->owner().'";':'";');
  exit($s);
 break;
 case 'json' :
  header ("Content-Type: text/javascript; charset=".ENCODE);
  if(is_file($smiley)){
   exit(get_file_content($smiley));
  } else {
   include_once (DIR.'owner/smiley.ini.php');
   $s = "var smilies = {\n";
   if(isset($_SMILIES)){
    foreach ($_SMILIES as $key=>$val) {$s .= "'$key':'$val',\n";}
   }
   $s = preg_replace("/,\n$/", "\n", $s);
   $s .= "};";
   rewrite($smiley, $s);
   exit(get_file_content($smiley));
  }
 break;
 case 'page' :
  if(!is_numeric(str_replace('d', '', $aim))) exit;
  $DIVISION['header'] = $DIVISION['body'] = '';
  $put = preg_replace('{^.*?<div class="article-content(?: shown-alone|)">(.*?)<\/div><!--#article-content-->.*$}s', '\\1', show_box($aim,0,'',1,0,0,0,0,1));
  exit((trim($DIVISION['script']) != '' ? $DIVISION['script']."\t\t" : '').$put);
 break;
 case 'article' :
  if(!is_numeric(str_replace('d', '', $aim))) exit;
  include_once ($theme_dir.'template.php');
  $DIVISION['header'] = $DIVISION['body'] = '';
  $put = article_nav($aim, FALSE, TRUE).'$'.show_box($aim,0,'',1,0,0,0,0,1);
  exit((trim($DIVISION['script']) != '' ? $DIVISION['script']."\t\t" : '').$put);
 break;
 case 'comments' :
  if(!is_numeric(str_replace('d', '', $aim))) exit;
  exit(show_box($aim,1,'',1,0,0,0,0,0,1));
 break;
 case 'commentform' :
  if(!is_numeric(str_replace('d', '', $aim))) exit;
  include_once (DIR.'modules/comment.inc.php');
  exit(comment_form($aim, TRUE, 0, 0, '', $cc_name, $cc_col, '', $cc_url));
 break;
 case 'trackback' :
  if(!is_numeric(str_replace('d', '', $aim))) exit;
  $DIVISION['body'] = $body = '';
  include_once (DIR.'modules/trackback.inc.php');
  exit(str_replace('id="trackback"', '', $DIVISION['body']));
 break;
 case 'cookie' :
  if(!is_numeric($aim)) exit;
  setcookie('PPBLOG_HELPER_STATE', v_('state'), time()+3600*24*30);exit;
 break;
 case 'log' :
  if(!is_numeric($aim)) exit;
  exit(get_file_content(LOG.$aim.EXT));
 break;
 case 'sharedlink' : case 'sbm' :
  if(!is_numeric($aim)) exit;
  if(isset($q[2]) && strstr($q[2], '=')) list(, $title) = explode('=', $q[2]);
  if(isset($q[3]) && strstr($q[3], '=')) list(, $link) = explode('=', $q[3]);
  exit(socialBookmark($aim, sanitize_data(rawurldecode($title)), sanitize_data($link), FALSE, TRUE));
 break;
 case 'sbm_add' :
  if(!is_numeric($aim)) exit;
  if(isset($q[2]) && strstr($q[2], '=')) list(, $sbm) = explode('=', $q[2]);
  $sbm_file = file(DB.'social_bookmarks');
  $hit = preg_grep("/$aim/", $sbm_file);
  if(!empty($hit)){
   $index = array_keys($hit); $index = $index[0];
   $hit = array_shift($hit);
   if(strpos($hit, $sbm)){
    $hit = preg_replace('/"'.$sbm.'":(\d+?),/e', "'\"$sbm\":'.('$1' + 1).','", $hit);
    $hit = preg_replace('/"total":(\d+?)\}/e', "'\"total\":'.('$1' + 1).'}'", $hit);
    $sbm_file[$index] = $hit;
    rewrite(DB.'social_bookmarks', $sbm_file); exit(1);
   }
  }
  exit(0);
 break;
}
?>